#define EN_INPUT  1
#include "t9_base.h"

static T9_STRUCT * t9_obj;
static BOOL if_inited = false;
static ErrorInfo err ;

/*------------------------------------
	public method area
*/


ErrorInfo
Error(const char err_str[]){
	printf("%s\n",(char *) err_str);
	return (ErrorInfo)err_str;
}

int 
CheckInput(char * test_input){
	for(;(uchar)(*test_input)  ; ){
		uchar temp = (uchar)*test_input;

		if (temp == 32 || temp == 35 || temp == 42){		
			
		}else{
			if (temp <48 ||  temp > 57){
				return false;
			}
		}


		test_input++;
	}
	return true;
}

BOOL
CheckIfNull(T9_STRUCT *t9){
	if(t9 != NULL ){
		return true;	
	}
	return false;
	
}


void 
py_init(){

	T9_STRUCT  * T9 = (T9_STRUCT *) malloc(sizeof(T9_STRUCT));
	T9->pysrf=T9PY;
    	T9->t9PY_addr=(T9_INDEX *)t9PY_index1;
    	T9->t9py=0;  
	T9->firstno=' ';
	T9->mblen =0;

	t9_obj = T9;
	if_inited = true;
	
}

void 
cn_log(OBJ_TYPE Type){
	if (Type == TYPE_STRING){
		printf("%s\n", t9_obj->t9PY_addr->PY_mb);	
	}else{
		uchar * str = _get_chinese_word(t9_obj);
		printf("%s\n", str);
	}
	
}
void 
py_input(char * dig_str){
	if(!if_inited){
		err = Error("Init Error");
	}
	if(!CheckInput(dig_str)){
		err = Error("input Error");
	}

	if (t9_obj == NULL ){
		err = Error("Init Point Error");
	}

	ulong py_hex;  // dig_hex example : 0x22 mean "22"
	uchar py_hex_first  = (uchar ) *(dig_str) - 48; // the first of dig_hex  0x2
	py_hex = char_to_hex(dig_str);
	printf("pri hex %lu, %u \n", 0x2243,0x2);
	printf("hex %lu , %u\n",py_hex,py_hex_first);

	Py_index_sub(py_hex,py_hex_first,0);
}

int 
MB_len(unsigned char* py_mb){
	int res = 0;
	for (;*(py_mb++)  ;res++);
	return res;
}


void *
py_out(int Type){
	if(!if_inited){
		err = Error("not initialization !");
	}
	if (Type == TYPE_STRING){
		return (uchar *)t9_obj->t9PY_addr->PY_mb;
	}else if (Type == TYPE_CN_WORD){
		uchar * one_word;
		one_word = _get_chinese_word(t9_obj);
		return (uchar *)one_word;
	}else{
		err = Error("No sucn type Error");
		return NULL;
	}
}

BOOL 
Choose(int statue,int l_or_r){

	if (statue == CHOOSE_MB){
		printf("mb len : %hhu \n mb index : %d \n",t9_obj->mblen,t9_obj->mb_cursor );
		if (l_or_r == LEFT && t9_obj->mb_cursor){
			t9_obj->t9PY_addr --;
			t9_obj->t9PY_addr->count = MB_len((uchar *)t9_obj->t9PY_addr->PY_mb)/3 ;
			t9_obj->chinese_cursor = 0;
			t9_obj->mb_cursor--;

			
		}else if (l_or_r == RIGHT && t9_obj->mb_cursor < t9_obj->mblen -1){
			t9_obj->t9PY_addr ++;
			t9_obj->t9PY_addr->count = MB_len((uchar *)t9_obj->t9PY_addr->PY_mb)/3 ;
			t9_obj->chinese_cursor = 0;
			t9_obj->mb_cursor++;
		}
	}else if (statue == CHOOSE_CHINESE) {
		if (l_or_r == LEFT && t9_obj->chinese_cursor){
			t9_obj->chinese_cursor--;
		}else if (l_or_r == RIGHT ){
			t9_obj->chinese_cursor = (t9_obj->chinese_cursor +1) % t9_obj->t9PY_addr->count; //循环右移动汉字
		}
	}else{
		return  true;	
	}
	return false;
}


/* -----------------------------

	private func area
*/

uchar *
_get_chinese_word( T9_STRUCT * T9){
	uchar * one_word;
	int No = T9->chinese_cursor;
	uchar  * py_mb = (uchar *)T9->t9PY_addr->PY_mb; 
	if (No*3 > MB_len((unsigned char *)py_mb)){
		return 0;
	}
	memcpy(one_word,py_mb+No*3,3);
	return one_word;
	 
}



ulong 
char_to_hex(char * inStr) {
	ulong res= 0;
	int count = 0;

	int len = MB_len((uchar *)inStr);        //
	int i = len;
	printf("%d len \t ",len);
	printf("%s \n",res);
	for(;count < len ;i--){				
		res =res + ((ulong)(*inStr) -48 ) *  ( ulong )pow(16 ,(i-1)) ;		//	
		printf("\n res : %lu \n",res);
		inStr++;
		count++;
	}
	return 	res;
}


// ulong 
// Get_T9_input(char * inStr){
// 	ulong res;
// 	int count = 0;
// 	uchar firstno = (uchar) *(inStr) - 48;
// 	int len = MB_len((uchar *)inStr);        //
// 	int i = len;
// 	for(;count < len ;i--){				
// 		res =res + ((ulong)(*inStr) -48 ) *  (unsigned long)pow(16 ,(i-1)) ;		//	
		
// 		inStr++;
// 		count++;
// 	}
// 	return 		
// 	printf("%lu , %u \n",res,firstno );
// 	Py_index_sub(res,firstno,0);

// 	uchar one_word[4];
// 	printf("%s\n", T9->t9PY_addr->PY_mb);
// 	Get_chinese_word(T9->chinese_cursor,one_word,T9->t9PY_addr->PY_mb);
	
// 	printf(" now  %d / %d : %s\n" ,T9->chinese_cursor, T9->t9PY_addr->count,one_word );
	
	

// 	Get_chinese_word(T9->chinese_cursor,one_word,T9->t9PY_addr->PY_mb);
// 	printf(" now  %d / %d : %s\n" ,T9->chinese_cursor, T9->t9PY_addr->count,one_word );
// 	Choose(CHOOSE_MB,RIGHT);
// 	printf("%s\n", T9->t9PY_addr->PY_mb);
// 	Get_chinese_word(T9->chinese_cursor,one_word,T9->t9PY_addr->PY_mb);
// 	printf(" now  %d / %d : %s\n" ,T9->chinese_cursor, T9->t9PY_addr->count,one_word );
// 	return res;							//
// }

/*******************************************************************************
**功能：输入法索引程序
**创建：magicchip
**日期：20080509
*******************************************************************************/
void 
Py_index_sub(unsigned long py, uchar  first, int index_number){
	uchar i;
	uchar flag = 0x55;
	unsigned long temp;
	uchar mblentemp;
	mblentemp = t9_obj->mblen;
	t9_obj->firstno =  first;
	t9_obj->t9py = py;
	t9_obj->mblen = 0x00;
	if ((t9_obj->pysrf == T9PY) && (t9_obj->firstno != ' '))		   //拼音输入法
	{
		for (i=0;i<t9PY_indexlen[t9_obj->firstno];i++)
		{
			if (t9_obj->t9py == (*(t9PY_index_headno[t9_obj->firstno]+i)).t9PY)
			{
				t9_obj->mblen++;
				t9_obj->mb_cursor=0;
				flag = 0xaa;
				t9_obj->t9PY_addr =(T9_INDEX *) (t9PY_index_headno[t9_obj->firstno]+i);
				t9_obj->t9PY_addr->count =  MB_len((uchar *)t9_obj->t9PY_addr->PY_mb)/3;
				for (i++;i<t9PY_indexlen[t9_obj->firstno];i++)
				{
					if (t9_obj->t9py == (*(t9PY_index_headno[t9_obj->firstno]+i)).t9PY)
						t9_obj->mblen++;
					else 
						break;
				}
				break;
			}
		}
		if (flag == 0x55)		   //没有查找完全对应的拼音组合，
		{
			// for (i=0;i<t9PY_indexlen[t9_obj->firstno];i++)
			// {
			// 	temp = (*(t9PY_index_headno[t9_obj->firstno]+i)).t9PY;
			// 	while (temp > t9_obj->t9py)
			// 	{
			// 		temp >>= 4;
			// 		if (temp == t9_obj->t9py){
			// 			t9_obj->t9PY_addr =(T9_INDEX *)( t9PY_index_headno[T9->firstno]+i);
			// 		 	t9_obj->mblen++;
			// 		 	i = t9PY_indexlen[t9_obj->firstno];
			// 		 	flag = 0xaa;
			// 		 	break;
			// 	 	}
			// 	}			 
			// }

			// if (flag == 0x55)
			// {
			// 	T9->t9py >>= 4;
			// 	T9->mblen = mblentemp;
			// }

			if (t9_obj->t9py > t9_obj->firstno){
				t9_obj->t9py >>= 4;
				Py_index_sub(t9_obj->t9py,t9_obj->firstno,0);
			}else{
				printf("Error . this is never been readched \n");
			}
			
		}
		if (index_number < t9_obj->mblen){
			t9_obj->t9PY_addr =  t9_obj->t9PY_addr + index_number;
		}

	}
	else if(t9_obj->pysrf == T9Dig)			 //数字输入
	{
		t9_obj->mblen++;
		t9_obj->t9PY_addr = (T9_INDEX *)&(t9PY_sz[t9_obj->firstno]);
	}
	else if(t9_obj->pysrf == T9Com)			//标点输入
	{
		t9_obj->mblen++;
		t9_obj->t9PY_addr = (T9_INDEX *)t9PY_index0;
	} 
	else if(t9_obj->pysrf == T9CharB)			//大写英文字母输入
	{
		if ((t9_obj->firstno>1)&&(t9_obj->firstno<10))
		{
			t9_obj->mblen++;
			t9_obj->t9PY_addr = (T9_INDEX *)&t9PY_ABC[t9_obj->firstno];
		}
	} 
	else if(t9_obj->pysrf == T9Char)			//小写英文字母输入
	{
		if ((t9_obj->firstno>1)&&(t9_obj->firstno<10))
		{
			t9_obj->mblen++;
			t9_obj->t9PY_addr = (T9_INDEX *)&t9PY_abc[t9_obj->firstno];
		}
	}
}
/*
int DrawChinese(unsigned char * chinese ) {
 	int i=VGA,j=VGAHI,k; 
	unsigned char mat[16][2];
	FILE *HZK; 
	if((HZK=fopen("hzk16","rb"))==NULL){
		exit(0); 
	}
	initgraph(&i,&j,""); 
	i=chinese[0]-0xa0;j=chinese[1]-0xa0; 
	
	fseek(HZK,(94*(i-1)+(j-1))*32l,SEEK_SET); 
	fread(mat,32,1,HZK); 
	for(j=0;j<16;j++){
		for(i=0;i<2;i++) {
			for(k=0;k<8;k++) {
				if(mat[j][i]&(0x80>>k)) { 				
					putpixel(i*8+k,j,WHITE); 
				}
			}			
		}	
	}
	




	getch(); 
	closegraph(); 
	fclose(HZK); 
}
*/

// test area --------------------------------

//int main(int argc, char const *argv[])
//{
	/* code */
//	py_init();
//	py_input(argv[1]);
//	Choose(CHOOSE_MB,RIGHT);
//	cn_log(TYPE_STRING);

	// py_init();
	// py_init();
	// cn_log(TYPE_STRING);
//	return 0;
//}

